﻿namespace Common
{
    using System;
    using System.Security.Cryptography;
    using System.Text;
    using System.Web.Security;

    public class EncryptDecrypt
    {
        private static string EncryptDecryptKey = "23q4p9irfdzflkgvlkj;er";

        public static string Decrypt3DES(string sSource)
        {
            TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
            provider.Key = new MD5CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(EncryptDecryptKey));
            provider.Mode = CipherMode.ECB;
            ICryptoTransform transform = provider.CreateDecryptor();
            string str = "";
            try
            {
                byte[] inputBuffer = Convert.FromBase64String(sSource);
                str = Encoding.Default.GetString(transform.TransformFinalBlock(inputBuffer, 0, inputBuffer.Length));
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return str;
        }

        public static string Decrypt3DES(string sSource, string EncryptDecryptKey)
        {
            TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
            provider.Key = new MD5CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(EncryptDecryptKey));
            provider.Mode = CipherMode.ECB;
            ICryptoTransform transform = provider.CreateDecryptor();
            string str = "";
            try
            {
                byte[] inputBuffer = Convert.FromBase64String(sSource);
                str = Encoding.Default.GetString(transform.TransformFinalBlock(inputBuffer, 0, inputBuffer.Length));
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return str;
        }

        public static string Decrypt3DES(string sSource, string EncryptDecryptKey, Encoding eEncoding)
        {
            TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
            provider.Key = new MD5CryptoServiceProvider().ComputeHash(eEncoding.GetBytes(EncryptDecryptKey));
            provider.Mode = CipherMode.ECB;
            ICryptoTransform transform = provider.CreateDecryptor();
            string str = "";
            try
            {
                byte[] inputBuffer = Convert.FromBase64String(sSource);
                str = eEncoding.GetString(transform.TransformFinalBlock(inputBuffer, 0, inputBuffer.Length));
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return str;
        }

        public static string Encrypt3DES(string sSource)
        {
            TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
            provider.Key = new MD5CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(EncryptDecryptKey));
            provider.Mode = CipherMode.ECB;
            ICryptoTransform transform = provider.CreateEncryptor();
            byte[] bytes = Encoding.Default.GetBytes(sSource);
            return Convert.ToBase64String(transform.TransformFinalBlock(bytes, 0, bytes.Length));
        }

        public static string Encrypt3DES(string sSource, string EncryptDecryptKey)
        {
            TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
            provider.Key = new MD5CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(EncryptDecryptKey));
            provider.Mode = CipherMode.ECB;
            ICryptoTransform transform = provider.CreateEncryptor();
            byte[] bytes = Encoding.Default.GetBytes(sSource);
            return Convert.ToBase64String(transform.TransformFinalBlock(bytes, 0, bytes.Length));
        }

        public static string Encrypt3DES(string sSource, string EncryptDecryptKey, Encoding eEncoding)
        {
            TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
            provider.Key = new MD5CryptoServiceProvider().ComputeHash(eEncoding.GetBytes(EncryptDecryptKey));
            provider.Mode = CipherMode.ECB;
            ICryptoTransform transform = provider.CreateEncryptor();
            byte[] bytes = eEncoding.GetBytes(sSource);
            return Convert.ToBase64String(transform.TransformFinalBlock(bytes, 0, bytes.Length));
        }

        public static string EncryptPassword(string PasswordString, string PasswordFormat)
        {
            if (PasswordFormat == "SHA1")
            {
                return FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString, "SHA1");
            }
            if (PasswordFormat == "MD5")
            {
                return FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString, "MD5");
            }
            return "";
        }
    }
}

